import session from '../storage/session'
import cookies from '../storage/cookies'
import api from "../../api";
import common from "../../assets/common";

const state = {
  token: '',
  userInfo: null,
  activeIndex: '',
  searchShow: 'show',
  config: {},
  articleHot: [],
  articleNew: [],
  articleLabel: [],
  articleSearch: [],
  commentNew: [],
  categoryArticle: [],
  categoryLabel: [],
  linkList: [],
}

const getters = {
  token: state => {
    return state.token || cookies.get('token')
  },
  userInfo: state => {
    return state.userInfo || (cookies.get('userInfo') ? JSON.parse(cookies.get('userInfo')) : null)
  },
}

const mutations = {
  // 触发方法 $store.commit('', data)
  SET_TOKEN: (state, data) => {
    state.token = data
    cookies.set('token', data)
  },
  SET_USER_INFO: (state, data) => {
    state.userInfo = data
    cookies.set('userInfo', JSON.stringify(data))
  },
  LOGOUT: (state) => {
    state.token = ''
    cookies.remove('token')
    cookies.remove('userInfo')
  }
}

const actions = {
  // 触发方法 $store.dispatch('', data)
  setLogin({commit, state}, data) {
    commit('SET_TOKEN', data.token)
    commit('SET_USER_INFO', data.userInfo)
  },
  setUserInfo({commit, state}, data) {
    commit('SET_USER_INFO', data)
  },
  setLogout({commit}) {
    commit('LOGOUT')
  },
  async getConfig() {
    if (Object.keys(state.config).length === 0) {
      // state.config = await api.config()
      state.config = {"allow_ip":"","app_id":"wx15d121723d648a01","app_secret":"76ea36e3aa75acb798e2022dde819317","article_hot_max":"6","article_new_max":"6","bloger_avatar":"  ","bloger_name":"bzing","bloger_sign":"不羡鸳鸯不羡仙。","copyright":"Copyright ©2017-2020","guestbook":"","label_max":"20","link_max":"10","login_fail_times":"5","page_size":"10","site_description":"php、python、mysql、jquery、html等技术经验分享，开源项目源码分享，与广大技术同学相互学习探讨","site_keywords":"itspire,php,python,jquery,mysql,html,微信开发,linux,rsa","site_name":"itSpire","site_url":"http://www.itspire.cn","site_version":"v2.0","trackcode":"皖ICP备16003411号-1"}
    }
  },
  async categoryArticle() {
    if (!state.categoryArticle.length) {
      // state.categoryArticle = await api.categoryList({type: 1})
      state.categoryArticle = [{"id":6,"categoryName":"PHP","count":37},{"id":7,"categoryName":"Python","count":13},{"id":8,"categoryName":"Golang","count":10},{"id":10,"categoryName":"数据库","count":9},{"id":11,"categoryName":"Linux","count":9},{"id":12,"categoryName":"前端","count":22},{"id":13,"categoryName":"其他","count":26}]
    }
  },
  async articleSearch() {
    if (!state.articleSearch.length) {
      // state.articleSearch = await api.articleSearch()
      state.articleSearch = [{"id":134,"title":"Golang 类型断言 x.(T) 以及 type switch 结构"},{"id":133,"title":"websocket 通信 Uncaught (in promise) DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state."},{"id":132,"title":"Vue 路由跳转页面不置顶"},{"id":131,"title":"PHPStorm Yaf框架代码追踪和提示"},{"id":130,"title":"Vue 首次加载很慢，vendor文件过大优化"},{"id":129,"title":"Webstorm 2020 永久激活破解"},{"id":128,"title":"诡异现象 只有一个接口出现跨域问题"},{"id":127,"title":"Dokcer  Error response from daemon: pull access denied for xxx, repository does not exist or may require 'docker login'"},{"id":126,"title":"Golang 读取开发环境和生产环境配置"},{"id":125,"title":"Golang 获取map类型的值报错: type interface {} does not support indexing"},{"id":124,"title":"Golang proxy.golang.org包访问失败解决方法"}]
    }
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
  modules: {}
})
